package com.p.lib.common.rx.subscriber;

import android.content.Intent;
import android.os.Bundle;

import com.p.lib.app.BaseApplication;
import com.p.lib.common.exception.BaseException;
import com.p.lib.common.rx.RxErrorHandler;

/**
 * Created by Administrator on 2017/5/26 0026.
 */
public abstract class ErrorHandlerSubscriber<T> extends DefualtSubscriber<T> {

    private RxErrorHandler rxErrorHandler;

    public ErrorHandlerSubscriber(RxErrorHandler rxErrorHandler) {
        this.rxErrorHandler = rxErrorHandler;
    }

    @Override
    public void onError(Throwable t) {

        t.printStackTrace();

        BaseException be = rxErrorHandler.handlerError(t);

        if(be.getCode() == BaseException.ERROR_OUT_LOGIN){
            //TODO 这里操作啊
            Intent intent = new Intent(BaseApplication.ACTION_LOGIN_EXPIRES_HINT);
            Bundle bundle = new Bundle();
            bundle.putString("LOGIN_HINT", be.getMessage());
            intent.putExtras(bundle);

            BaseApplication.getInstance().sendBroadcast(intent);

        }else{

            rxErrorHandler.showMessage(be);

        }

    }
}
